/**
 * Web.java
 * 作者：IccBoY
 * 2017年6月5日 创建文件
 */
package com.iccboy.cupload.web;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.iccboy.cupload.service.UploadService;
import com.iccboy.cupload.util.HttpUtil;

/**
 **************************************************
 * 在此添加功能描述
 * 
 * @version 1.0.0
 * @author  iccboy
 **************************************************
 */
public class Web extends HttpServlet {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 4731755721335957649L;
	
	/**
	 * LOG
	 */
	private static final Logger LOG = LoggerFactory.getLogger(Web.class);
	
	public static final String LIST = UploadService.LIST;

	public static final String FORM = UploadService.FORM;

	public static final String HANDLE = UploadService.HANDLE;

	public static final String DOWNLOAD = UploadService.DOWNLOAD;

	public static final String DELETE = UploadService.DELETE;

	public static final String UPLOAD_DIR = UploadService.UPLOAD_DIR;
	
	private UploadService uploadService = new UploadService();
	

	/**
	 * 执行方法
	 * @param request request
	 * @param response response
	 * @throws ServletException ServletException
	 * @throws IOException IOException
	 * @since 1.0.0
	 * @author iccboy 2017年6月5日 创建方法
	 */
	public void execute(HttpServletRequest request, HttpServletResponse response) {
		try {
			LOG.info("execute...");
			LOG.info("------------begin---------------");
			request.setCharacterEncoding("UTF-8");
			String host = HttpUtil.getIpAddr(request);
			LOG.info("host:" + host);
			uploadService.checkIpAddr(request);
			String uri = request.getRequestURI();
			LOG.info("uri:" + uri);
			ServletContext servletContext = this.getServletConfig().getServletContext();
			// 上传文件的基本路径
			String basePath = UPLOAD_DIR;
			// 上下文路径
			String contextPath = servletContext.getContextPath();
			LOG.info("contextPath:" + contextPath);
			// 截取上下文之后的路径
			String action = uri.substring(contextPath.length());
			LOG.info("action:" + action);
			// 依据action不同进行不同的处理
			if (action.equals(FORM)) {
				uploadService.form(contextPath, response);
			} else if (action.equals(HANDLE)) {
				uploadService.handle(contextPath, basePath, request, response);
			} else if (action.equals(LIST) | "/".endsWith(action) ) {
				uploadService.list(contextPath, basePath, request, response);
			} else if (action.equals(DOWNLOAD)) {
				uploadService.download(contextPath, basePath, request, response);
			} else if (action.equals(DELETE)) {
				uploadService.delete(contextPath, basePath, request, response);
			} else {
				uploadService.show404(response);
			}
			
		} catch(Exception e) {
			LOG.error("execute", e);
			uploadService.show500(response, e.getMessage());
		} finally {
			LOG.info("------------end---------------");
		}
	}

	/**
	 * doGet
	 */
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
		execute(req, resp);
	}

	/**
	 * doPost
	 */
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
		execute(req, resp);
	}
}
